import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:sptapp/api/interface/user_api.dart';
import 'package:sptapp/api/request/response_data.dart';
import 'package:sptapp/widget/common_widget.dart';

import '../../config/const.dart';

class MinAddMemberDialog extends StatefulWidget {
  final Function callBack;
  const MinAddMemberDialog({super.key, required this.callBack});

  @override
  State<MinAddMemberDialog> createState() => _MinAddMemberDialogState();
}

class _MinAddMemberDialogState extends State<MinAddMemberDialog> {

  String? email;

  TextEditingController _controller = TextEditingController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  _addUser()async{
    if(email == null || email?.length == 0 ){
      return;
    }
    Map<String,dynamic> data = {
      'email':email,
    };
    showLoading();
    ResponseData res = await UserAPI.invitationUser(data);
    hideLoading();
    if(res.code == 200 && res.data != null){
      if(res.data['status']!=null && res.data['status'].toString() == '0'){
        if(widget.callBack!=null){
          widget.callBack();
        }
        SmartDialog.dismiss();
      }else if(res.msg != null){
        showToast(res.msg);
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: GestureDetector(
        onTap: (){
          FocusScope.of(context).requestFocus(FocusNode());
        },
        child: Container(
          color: Colors.transparent,
          width: 1.sw,
          height: 1.sh,
          alignment: Alignment.topCenter,
          child: Container(
            width: 680.w,
            margin: EdgeInsets.only(top: 340.w),
            padding: EdgeInsets.symmetric(horizontal: 40.w),
            decoration: BoxDecoration(
                color: Color(0xff1e1d20),
                borderRadius: BorderRadius.circular(16.w)
            ),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Container(
                  height: 112.w,
                  decoration: BoxDecoration(
                      border: Border(bottom: BorderSide(color: Color.fromRGBO(255, 255, 255, 0.1),width: 1.w))
                  ),
                  child: Stack(
                    children: [
                      Container(
                        height: 112.w,
                        alignment: Alignment.center,
                        child: Text('Add Member'.tr,style: TextStyle(
                            color: Colors.white,
                            fontSize: 32.sp,
                            fontWeight: FontWeight.w600
                        ),),
                      ),
                      Positioned(child: GestureDetector(
                        onTap: (){
                          SmartDialog.dismiss();
                        },
                        child: Container(
                          padding: EdgeInsets.only(top: 24.w,),
                          child: Image.asset('assets/common/common_close.png',width: 48.w,),
                        ),
                      ),top: 0,right: 0,)
                    ],
                  ),
                ),
                Container(
                  margin: EdgeInsets.symmetric(vertical: 40.w),
                  alignment: Alignment.center,
                  child: Text('New users will need to create a SPT Account before you can add their profile to your system'.tr,style: TextStyle(
                      color: Colors.white,
                      fontSize: 24.sp,
                      fontWeight: FontWeight.w400
                  ),),
                ),
                Container(
                  height: 108.w,
                  padding: EdgeInsets.symmetric(horizontal: 30.w),
                  decoration: BoxDecoration(
                      border: Border.all(color: Color.fromRGBO(255, 255, 255, 0.5),width: 1.w),
                      borderRadius: BorderRadius.circular(20.w)
                  ),
                  child:  Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Expanded(child: TextField(
                        //控制器
                        textInputAction: TextInputAction.done,
                        controller: _controller,
                        maxLength: 20,
                        maxLines: 1,
                        //是否自动更正
                        autocorrect: true,
                        //是否自动对焦
                        autofocus: false,
                        style: inputStyle,
                        inputFormatters: [

                        ],
                        decoration: InputDecoration(
                          contentPadding: const EdgeInsets.symmetric(vertical: 0,horizontal: 0),
                          hintText: 'Invite user\'s email'.tr,
                          hintStyle: inputHitStyle,
                          counterText: "",
                          border: OutlineInputBorder(
                              borderSide: BorderSide.none),
                        ),
                        onChanged: (val){
                         setState(() {
                           email = val.trim();
                         });
                        },
                      )),
                      SizedBox(width: 20.w,),
                      Visibility(visible: true,
                        child: GestureDetector(
                          onTap: (){
                            setState(() {
                              email = null;
                              _controller.text = '';
                            });
                          },
                          child: Container(
                            child: inputImg,
                          ),
                        ),
                      )
                    ],
                  ),
                ),
                SizedBox(height: 40.w,),
                GestureDetector(
                  onTap: (){
                    FocusScope.of(context).requestFocus(FocusNode());
                    _addUser();
                  },
                  child: Container(
                    child: Text('Continue'.tr,style: TextStyle(
                        color: Colors.white,
                        fontSize: 28.sp
                    ),),
                    decoration: BoxDecoration(
                        color:  email == null || email?.length == 0 ? themeBlueDisableColor :  themeColor,
                        borderRadius: BorderRadius.circular(8.w)
                    ),
                    height: 108.w,
                    alignment: Alignment.center,
                  ),
                ),
                SizedBox(height: 40.w,),
              ],
            ),
          ),
        ),
      ),
    );
  }
}
